import java.util.Random;

/**
 * 加减法口算练习系统 V02
 * 功能：
 * 1. 生成50道加减法算式习题
 * 2. 每个算式显示答案
 * 3. 加法算式的和不超过100
 * 4. 减法算式的差不小于0
 * 5. 每行显示多个算式，整齐排列
 */
public class CalculationSystemV02 {
    
    // 每行显示的算式数量
    private static final int EXERCISES_PER_ROW = 3;
    
    public static void main(String[] args) {
        System.out.println("加减法口算练习系统 V02");
        System.out.println("======================\n");
        
        // 生成50道习题
        generateExercises(50);
    }
    
    /**
     * 生成指定数量的加减法习题
     * @param count 习题数量
     */
    public static void generateExercises(int count) {
        Random random = new Random();
        StringBuilder rowBuilder = new StringBuilder();
        
        for (int i = 0; i < count; i++) {
            // 随机生成加减法类型 (0: 加法, 1: 减法)
            int operation = random.nextInt(2);
            
            // 根据运算类型生成合适的操作数和答案
            Exercise exercise = generateExerciseWithAnswer(operation, random);
            
            // 格式化算式字符串，包括答案，总宽度约30个字符
            String formattedExercise = String.format("%2d. %-25s", (i + 1), exercise.toString());
            rowBuilder.append(formattedExercise);
            
            // 每行显示指定数量的算式
            if ((i + 1) % EXERCISES_PER_ROW == 0) {
                System.out.println(rowBuilder.toString());
                rowBuilder.setLength(0); // 清空StringBuilder
            } else if (i < count - 1) {
                rowBuilder.append("  "); // 算式之间的间隔
            }
        }
        
        // 如果最后一行不足指定数量的算式，也需要输出
        if (rowBuilder.length() > 0) {
            System.out.println(rowBuilder.toString());
        }
    }
    
    /**
     * 根据运算类型生成一道习题及答案
     * @param operation 运算类型 (0: 加法, 1: 减法)
     * @param random 随机数生成器
     * @return 生成的习题对象，包含算式和答案
     */
    public static Exercise generateExerciseWithAnswer(int operation, Random random) {
        int num1, num2, answer;
        String operator;
        
        if (operation == 0) { // 加法
            operator = "+";
            // 为保证结果在100以内，两个数之和不超过100
            num1 = random.nextInt(100); // 0-99
            num2 = random.nextInt(101 - num1); // 保证num1 + num2 <= 100
            answer = num1 + num2;
        } else { // 减法
            operator = "-";
            // 为保证结果为非负数，被减数大于等于减数
            num1 = random.nextInt(100) + 1; // 1-100
            num2 = random.nextInt(num1 + 1); // 0-num1
            answer = num1 - num2;
        }
        
        return new Exercise(num1, num2, operator, answer);
    }
    
    /**
     * 习题类，用于存储算式的各个部分和答案
     */
    static class Exercise {
        private int num1;
        private int num2;
        private String operator;
        private int answer;
        
        public Exercise(int num1, int num2, String operator, int answer) {
            this.num1 = num1;
            this.num2 = num2;
            this.operator = operator;
            this.answer = answer;
        }
        
        @Override
        public String toString() {
            // 格式：数字1 运算符 数字2 = 答案
            return num1 + " " + operator + " " + num2 + " = " + answer;
        }
    }
}